package com.demo.t072401.scheduled;

import com.demo.t072401.pojo.GwrdpRobotData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author：wsq
 * @Date：2023/8/23 14:44
 */
@Slf4j
@Configuration
@EnableScheduling
public class TestSchedulingTask3 {

    int i = 0; // 用来记录周几,方便后面以7天为一个周期覆盖前面文件。
    String format; // 日期格式

    static String baseDir = System.getProperty("user.dir");

    static {
        // 文件保存路径，最后面的1没有用处，但是不加上只会创建到工作单元1目录。
        File file1 = new File(baseDir+"\\产线1\\工作单元1\\设备1\\1");
        // 创建保存目录
        if (!file1.exists()) {
            File file2 = new File(file1.getParent());
            file2.mkdirs();
            try {
                file2.createNewFile();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    // 这里cron格式用作测试，按照要求应替换成 @Scheduled(cron = "0 0 23 * * ?") 即每天的23点运行
    @Scheduled(cron = "0/3 * * * * ?")
    public void test() {
        // 当改天是周日时，重置到周一
        if (i == 7) {
            i = 0;
        }
        // 天数递增
        i++;

        Date date = new Date(System.currentTimeMillis());
        String rule = "yyyy-MM-dd-" + i;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(rule);
        format = simpleDateFormat.format(date);

        // 文件保存路径
        File file = new File(baseDir+"\\产线1\\工作单元1\\设备1\\" + format + ".backup");
        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(file));
            GwrdpRobotData userEntity = new GwrdpRobotData();
            userEntity.setId(1);
            userEntity.setAxis1Current(125.2);
            userEntity.setAxis2Current(163.1);
            userEntity.setAxis3Current(142.8);
            userEntity.setAxis4Current(126.7);
            userEntity.setAxis5Current(175.1);
            userEntity.setAxis6Current(156.6);

            userEntity.setAxis1Torque(132.4);
            userEntity.setAxis2Torque(168.9);
            userEntity.setAxis3Torque(148.7);
            userEntity.setAxis4Torque(130.2);
            userEntity.setAxis5Torque(180.5);
            userEntity.setAxis6Torque(161.3);

            userEntity.setAxis1Position(45.2);
            userEntity.setAxis2Position(90.5);
            userEntity.setAxis3Position(135.8);
            userEntity.setAxis4Position(180.0);
            userEntity.setAxis5Position(225.3);
            userEntity.setAxis6Position(270.6);

            userEntity.setAxis1Temperature(129.7);
            userEntity.setAxis2Temperature(167.8);
            userEntity.setAxis3Temperature(146.2);
            userEntity.setAxis4Temperature(129.8);
            userEntity.setAxis5Temperature(179.6);
            userEntity.setAxis6Temperature(60.7);

            userEntity.setRobotSpeed(15);
            userEntity.setRobotAlarm(0);
            userEntity.setRobotVoltage(220);
            userEntity.setRobotNumber("S52364222333");
            userEntity.setRobotIp("192.168.3.36");
            userEntity.setRobotName("设备1");

            objectOutputStream.writeObject(userEntity);
            objectOutputStream.close();

            System.out.println("write done 第" + i + "次");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
